<?php

class Lister {
	
	function __construct() {
		if (isset($_SESSION['limit'])) {
			$this->limit = $_SESSION['limit'];
		}
	}

	
	function category_selectbox($findBy = "id", $id = "category") {
		$query = "SELECT category.category_id, category.name FROM category";
		$result = mysql_query($query);
		echo "<label id=\"{$id}\">Category " .
				 "<select name=\"category\" id=\"category_selectbox\">";
		$category = stripslashes($id);
		//echo "<option value=\"$category\">$category</option>";
		if ($findBy == "name") {
			while($option=mysql_fetch_array($result)){
				echo "<option value=\"$option[name]\">$option[name]</option>";
			}
		} 
		if ($findBy == "id") {
			while($option=mysql_fetch_array($result)){
				echo "<option value=\"$option[category_id]\">$option[name]</option>";
			}
		}
		echo "</select>";
		echo "</label>";
	}

	function categories() {
		$query = "SELECT category.category_id, category.name FROM category";
		$result = mysql_query($query);
		echo "<div id=\"category-links\">";
		while($category=mysql_fetch_array($result)){
			echo bold("<a href=\"index.php?category={$category['name']}\">{$category['name']}</a> | ");
		}
		echo "</div>";
	}

	function categories_with_links() {
		$query = "SELECT category.category_id, category.name FROM category";
		$result = mysql_query($query);
		while($category=mysql_fetch_array($result)){
			echo bold("{$category['name']}");
			list_links_for_category($category['name']);
		}
	}

/* can find by "id" and "name" */
	function links_for_category($findBy = "name", $category = "", $start = "0", $limit = "5") {
		
		$query = "SELECT * FROM links";
		if (!empty($category) && $category != "Latest Links") {
			switch ($findBy) {
				case "id":
					$query .= " LEFT JOIN category
				ON category.category_id = links.category_id WHERE category.category_id = '".$category."'";
				break;
				case "name":
					$query .= " LEFT JOIN category
					ON category.category_id = links.category_id WHERE category.name = '".$category."'";
					break;
			}
		}	else {
				$query .= "";
				$category = "Latest Links";
		}
		
		// Report back the number of rows total
		$this->numRowsTotal = $this->numRows($query);
		$query .= " LIMIT {$start} , {$limit}";
		// after start and limit, calculates # of rows received
		$this->numRowsReceived = $this->numRows($query);
		
		$this->start = $start;
		$this->results = $this->start. " - ". ($this->start + $this->numRowsReceived);
			//echo $query;
		
		//echo paragraph($query);
		$category = stripslashes($category);
		echo "<h3>{$category}</h3>";
		$result = mysql_query($query);
		$this->numRows = mysql_num_rows($result);
		echo "<ul id=\"link_list\">";
		while($field=mysql_fetch_array($result)){
			echo "<li><a href=\"{$field['url']}\">{$field['title']}</a>";
			if(!empty($field['description'])) echo " - {$field['description']}";
			echo "</li>";
		}
		echo "</ul>";
		
		echo $this->paginate($this->numRowsReceived, $this->numRowsTotal, $start, $limit, $category);
		echo "<div align=\"center\">";
		echo "{$this->results} of {$this->numRowsTotal} total links returned.";
		echo "</div>";
		
	}
	
	function numRows($query) {
		$result = mysql_query($query);
		return mysql_num_rows($result);
	}
	
	function paginate($numRowsReceived, $numRowsTotal, $start, $limit, $category = "") {
			$back = $start - $limit;
			$next = $start + $limit;
			?>
			<div align="center">
				<?php if ($start > 0): ?>
						<a href="http://localhost/post-a-link/index.php?limit=<?php echo $limit ?>&start=<?php echo $back ?>&category=<?php echo $category ?>&filter=Submit"><< Back</a> |
				<?php endif;
				if ($numRowsTotal > ($start + $numRowsReceived)):?>
						<a href="http://localhost/post-a-link/index.php?limit=<?php echo $limit ?>&start=<?php echo $next ?>&category=<?php echo $category ?>&filter=Submit">Next >></a>
				<? endif; ?>
			</div>
			<?
		
	}
}
?>